package com.yz.android_baozou.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yz.android_baozou.R;
import com.yz.android_baozou.activity.ColumnDetailList;
import com.yz.android_baozou.activity.MainPageDetailActivity;
import com.yz.android_baozou.bean.ColumnBean;
import com.yz.android_baozou.helper.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/11/2.
 */

public class Column_RecyclerAdapter extends RecyclerViewAdapterHelper<ColumnBean.DataBean> {
    //声明RecyclerView
    private RecyclerView recyclerViewColumnMain;

    //构造方法
    public Column_RecyclerAdapter(Context context, List<ColumnBean.DataBean> list, RecyclerView _recyclerViewColumnMain) {
        super(context, list);
        this.recyclerViewColumnMain = _recyclerViewColumnMain;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        //根据布局管理器获得item布局
        View view = inflater.inflate(R.layout.item_column_main, parent, false);
        //给view设置监听
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //借助recyclerView获得点击的item的下标
                int position = recyclerViewColumnMain.getChildAdapterPosition(v);
                //获得item的id
                int id = list.get(position).getId();
                //获得item的name
                String title = list.get(position).getName();
                //借助bundle携带数据
                Bundle bundle = new Bundle();
                bundle.putString("title", title);
                bundle.putInt("id", id);
                //创建intent对象 携带bundle 并且进行跳转
                Intent intent = new Intent(context, ColumnDetailList.class);
                intent.putExtra("id", bundle);
                //根据intent跳转页面
                context.startActivity(intent);
            }
        });
        return new MyViewHolder(view);
    }

    @Override
    //绑定item
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            ((MyViewHolder) holder).textView_title_column.setText(list.get(position).getName());
            ((MyViewHolder) holder).textView_content_column.setText(list.get(position).getSummary());
            Glide.with(context).load(list.get(position).getImage()).into(((MyViewHolder) holder).imageView_show_column);
        }
    }

    //生成item的实体类
    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView_show_column;
        private TextView textView_title_column;
        private TextView textView_content_column;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView_show_column = (ImageView) itemView.findViewById(R.id.imageView_show_column);
            textView_title_column = (TextView) itemView.findViewById(R.id.textView_title_column);
            textView_content_column = (TextView) itemView.findViewById(R.id.textView_content_column);
        }
    }
}
